package com.lanou.dllo.soccernews.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.Attachments;
import com.lanou.dllo.soccernews.bean.DataResult;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class HotAdapter extends BaseAdapter {
    private LayoutInflater inflater = null;
    private List<DataResult> dataResults;
    private Context context;
    private String username, title, total_replise, content;

    public HotAdapter(Context context) {
        this.content = content;
        inflater = LayoutInflater.from(context);
    }

    public void addDatas(List<DataResult> d) {
        this.dataResults = d;
        notifyDataSetChanged();

    }


    @Override
    public int getCount() {
        return dataResults != null && dataResults.size() > 0 ? dataResults.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataResults.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHodle hodle;

        if (convertView == null) {
            hodle = new ChildHodle();
            convertView = inflater.inflate(R.layout.item_hotchild, null);
          //  Log.i("lanou", content + "yyyyyyyyyyyy");
            hodle.hotTime = (TextView) convertView.findViewById(R.id.one_status_name);

            hodle.hotContent = (TextView) convertView.findViewById(R.id.hot_content);
            hodle.hotTitle = (TextView) convertView.findViewById(R.id.hot_title);
            hodle.hotTotalReplies = (TextView) convertView.findViewById(R.id.hot_total_replies);
            hodle.hotUserName = (TextView) convertView.findViewById(R.id.hot_username);
            hodle.hotContent = (TextView) convertView.findViewById(R.id.hot_content);
            hodle.hotThumb = (ImageView) convertView.findViewById(R.id.hot_thumb);
            hodle.hotThumb2 = (ImageView) convertView.findViewById(R.id.hot_thumb2);
            hodle.hotThumb3 = (ImageView) convertView.findViewById(R.id.hot_thumb3);


            convertView.setTag(hodle);
        } else {

            hodle = (ChildHodle) convertView.getTag();
        }


        DataResult datas = dataResults.get(position);

        if (datas != null) {
            hodle.hotTitle.setText(datas.getGroup().getTitle());
            hodle.hotUserName.setText(datas.getAuthor().getUsername());
            hodle.hotTotalReplies.setText(datas.getTotal_replies() + "");
            hodle.hotContent.setText(datas.getContent());
            hodle.hotTime.setText(datas.getCreated_at());


            List<Attachments> attachmentsList = datas.getAttachments();
            switch (attachmentsList.size()) {
                case 0 :
                    break;
                case 1 :
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), hodle.hotThumb);
                    break;
                case 2 :
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), hodle.hotThumb);
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(1).getThumb(), hodle.hotThumb2);
                    break;
                case 3 :
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(0).getThumb(), hodle.hotThumb);
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(1).getThumb(), hodle.hotThumb2);
                    GetVolleyDatasUtil.getNetWorkImg(attachmentsList.get(2).getThumb(), hodle.hotThumb3);
                    break;
            }

        }


        return convertView;
    }


    class ChildHodle {

        TextView hotTime, hotContent, hotUserName, hotTitle, hotTotalReplies;

        ImageView hotThumb, hotThumb2, hotThumb3;

        ImageView imgs[] = new
                ImageView[]{hotThumb, hotThumb2, hotThumb3};

    }
}
